const { verifyJwtToken } = require('../util/server_jwt');

// 验证卡密的中间件
async function checkToken(req, res, next) {
    let token = req.headers.authorization;
    if (!token) {
        res.status(401).json({ code: 401, msg: "令牌不能为空", data: null });
        return ;
    }
    // 验证令牌
    let result = await verifyJwtToken(token);
    if (result.data) {
        req.uid = result.data.uid;
        req.userTypeId = result.data.userTypeId;
        next();
    } else {
        res.status(401).json({
            code: 401,
            msg: "令牌验证失败",
            data: null
        });
    }
}

module.exports = checkToken;